
$(document).ready(function() {
    var userKey = Utils.getUrlParam("userName");
	var keyType = 1;
	var phone = "";
	var captchaCode = "";
	$("#user-name").val(userKey);
	
	/* 输入用户名：点击下一步 */
	$("#name-next-step").click(function() {
        userKey = $("#user-name").val();
		
		var numReg = /^[0-9]+$/;
		var phoneReg = /^[1][34578][0-9]{9}$/;
		var mailReg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
		var nameReg = /^[\u4E00-\u9FA50-9A-Za-z_-]{3,25}$/;
		if(numReg.test(userKey)) {
			keyType = 2;
			if (!phoneReg.test(userKey)) {
            	$("#error-msg-tips").text("手机号码格式不正确！");
            	return;
        	}
		} else if(userKey.indexOf('@') >= 0) {
			keyType = 3;
			if(!mailReg.test(userKey)) {
            	$("#error-msg-tips").text("邮箱格式不正确！");
				return;
			}
		} else {
			keyType = 1;
			if(!nameReg.test(userKey)) {
            	$("#error-msg-tips").text("用户名格式不正确！");
				return;
			} else if(Utils.countLength(userKey) < 5 || Utils.countLength(userKey) > 25) {
            	$("#error-msg-tips").text("用户名长度不合法！");
				return;
			}
		}
		
        var param = {
            userKey: userKey,
            keyType: keyType,
        };
        AQ.callAQ("isUserKeyUsed.do", param, function(result) {
			$("#error-msg-tips").text("登录名不正确！");
        }, function(err, code) {
			if(code == 2) {
				setUserPhone();
				$(".login-box").each(function(index, element) {
                    if(index == 1) {
						$(element).show();
					} else {
						$(element).hide();
					}
                });
			} else {
				$("#error-msg-tips").text("登录名不正确！");
			}
        });
	});
	
	/* 显示用户手机号 */
	function setUserPhone() {
		var param = {
			userKey: userKey,
			keyType: keyType,
			propNo : 1,
		};
		AQ.callAQ("getUserPropByKey.do", param, function(result) {
			var data = result[1];
			$("#user-phone").text(data.phone);
		}, function(err, code) {
			Utils.msgDialog(err +"["+ code +"]");
		});
	}
	
	/* 验证身份：发送验证码 */
	$("#send-code").click(function() {
		var param = {
			captchaType: 1,
			captchaWay: 0,
			captchaUse: 607,
			userKey: userKey,
            keyType: keyType,
		};
		AQ.callAQ("applyCaptcha.do", param, function(result) {
			countDown($("#send-code")[0], 60);
		}, function(err, code){
			Utils.msgDialog(err + "["+ code +"]");
		});
    });
	
	/* 验证身份：点击下一步 */
	$("#yz-next-step").click(function() {
		captchaCode = $.trim($("#captcha-code").val());
		var codeReg = /^[0-9]{6}$/;
		if(!codeReg.test(captchaCode)) {
			Utils.msgDialog("请输入正确的验证码！");
			return;
		}
		
        var param = {
			captchaCode: captchaCode,
			captchaUse: 607,
			userKey: userKey,
            keyType: keyType,
		};
		AQ.callAQ("validateCaptcha.do", param, function(result) {
			$(".login-box").each(function(index, element) {
                    if(index == 2) {
						$(element).show();
					} else {
						$(element).hide();
					}
                });
		}, function(err, code){
			Utils.msgDialog(err + "["+ code +"]");
		});
    });
	
	/* 重置密码：点击下一步 */
	$("#reset-next-step").click(function() {
		var pwd = $("#new-pwd").val();
		var r_pwd = $("#re-new-pwd").val();
		var pwdReg = /^[0-9A-Za-z!@#$%&*_-]{6,25}$/;
		if(!pwdReg.test(pwd)) {
			$("#pwd-err-tips").text("× 密码格式不正确");
			return;
		} else if(!(pwd == r_pwd)) {
			$("#pwd-err-tips").text("× 两次密码不一致");
			return;
		}
		
        var param = {
			passWord: pwd,
			captchaCode: captchaCode,
			userKey: userKey,
            keyType: keyType,
		};
		AQ.callAQ("resetPwd.do", param, function(result) {
			$(".login-box").each(function(index, element) {
				if(index == 3) {
					$(element).show();
				} else {
					$(element).hide();
				}
			});
		}, function(err, code){
			Utils.msgDialog(err + "["+ code +"]");
		});
	});
	
	/* 密码修改成功：点击登录 */
	$("#go-login").click(function() {
		location.replace("login.html");
	});
	
	
});
